package com.rc.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.dao.tbl_Report;
import com.rc.activity.provider.Contract;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;
import com.telpoo.frame.ui.BaseActivity;

public class ReportCreateNew extends BaseActivity implements OnClickListener {

	private LinearLayout llSave, llCancel, llReset;
	private TextView txtReportName, txtDate, txtSubmitter, txtApprover,
			txtReference;
	// Calendar
	private Calendar cal;
	private SimpleDateFormat dft = null;

	private Cursor cursor = null;
	private String keyReport = "";

	public static void callIntent(Context ct, String keyReport) {
		Intent it = new Intent(ct, ReportCreateNew.class);
		it.putExtra("KEY_REPORT", keyReport);
		ct.startActivity(it);
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.report_create_new);
		((TextView) findViewById(R.id.lbTitle))
				.setText(getString(R.string.edit_report_info));
		llSave = (LinearLayout) findViewById(R.id.llSave);
		llCancel = (LinearLayout) findViewById(R.id.llCancel);
		llReset = (LinearLayout) findViewById(R.id.llReset);
		llSave.setOnClickListener(this);
		llCancel.setOnClickListener(this);
		txtReportName = (TextView) findViewById(R.id.txtReportName);
		txtDate = (TextView) findViewById(R.id.txtDate);
		txtSubmitter = (TextView) findViewById(R.id.txtSubmitter);
		txtApprover = (TextView) findViewById(R.id.txtApprover);
		txtReference = (TextView) findViewById(R.id.txtReference);

		llReset.setOnClickListener(this);
		llCancel.setOnClickListener(this);
		llSave.setOnClickListener(this);
		txtDate.setOnClickListener(this);

		// set default date for TEXTVIEW DATE
		getDefaultInfor();

		// get data intent
		if (getIntent() != null) {
			keyReport = getIntent().getStringExtra("KEY_REPORT");
		}

	}

	/**
	 * update data from database local
	 */
	private void InsertData() {
		tbl_Report objReport = new tbl_Report();
		objReport.setReportID(Long.valueOf(Util.getCurrentTime()));
		objReport.setIsCreate("1");
		objReport.setReportType(getString(R.string.draft_reports));
		objReport.setIsArchived(0);
		objReport.setReference(txtReference.getText().toString());
		objReport.setCreatedTime(Util.getDefaultInfor());
		objReport.setTitle(txtReportName.getText().toString());
		objReport.setIsDelete("");
		objReport.setAmount("0.00");
		objReport.setApproved("0.00");
		objReport.setClaimed("0.00");
		objReport.setDate(txtDate.getText().toString());
		objReport.setApproverEmail(txtApprover.getText().toString());
		this.getContentResolver().insert(Contract.CONTENT_URI_REPORT,
				objReport.getContentValues());
	}

	private void getDefaultInfor() {
		cal = Calendar.getInstance();
		dft = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
		String strDate = dft.format(cal.getTime());
		txtDate.setText(strDate);
		dft = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
		String reference = "R" + dft.format(cal.getTime());
		txtReference.setEnabled(false);
		txtReference.setText(DatabaseSupport.validateReferenceReport(reference,
				ReportCreateNew.this));
		txtSubmitter.setText(DatabaseSupport.getTbl_User(ReportCreateNew.this)
				.getEmail());
	}

	/**
	 * check update is true
	 */

	private boolean CheckInsert() {
		if (!Util.validateText(txtReportName)) {
			Util.setError((EditText) txtReportName,
					getString(R.string.input_report_name));
		} else {
			return true;
		}
		return false;
	}

	/**
	 * display dialog datepiker
	 */

	public void showDatePickerDialog() {
		int date = 0, month = 0, year = 0;

		OnDateSetListener callback = new OnDateSetListener() {
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				// Date
				txtDate.setText((dayOfMonth) + "-" + (monthOfYear + 1) + "-"
						+ year);
				dft = new SimpleDateFormat("yyyyMMdd", Locale.getDefault());
				String reference = "R" + dft.format(cal.getTime());
				txtReference.setEnabled(false);
				txtReference.setText(DatabaseSupport.validateReferenceReport(
						reference, ReportCreateNew.this));
				cal.set(year, monthOfYear, dayOfMonth);
			}
		};
		String s = txtDate.getText() + "";
		if (s.indexOf("-") >= 0) {
			String strArrtmp[] = s.split("-");
			date = Integer.parseInt(strArrtmp[0]);
			month = Integer.parseInt(strArrtmp[1]) - 1;
			year = Integer.parseInt(strArrtmp[2]);
			DatePickerDialog pic = new DatePickerDialog(ReportCreateNew.this,
					callback, year, month, date);
			pic.setTitle(getString(R.string.choose_calendar));
			pic.show();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.llCancel:
			finish();
			break;
		case R.id.llSave:
			if (CheckInsert()) {
				InsertData();
				ReceiptBox.callIntent(ReportCreateNew.this, keyReport, 3);
			}
			break;
		case R.id.llReset:
			break;
		case R.id.txtDate:
			showDatePickerDialog();
			break;
		default:
			break;
		}
	}

}
